(function($, doc) {
	$.init();
	$.plusReady(function() {
		var user = app.getState();
		var uid = user.user_id;
		var postpass = md5(user.password);
		var self = plus.webview.currentWebview();
		var token = self.token;
		var id = self.dataid;
		var coindata = ethscan.getCoindata(token);
		var coinname = coindata['name'];
		var ctype = coindata['ctype'];
		var myWallet = ethscan.getWallet(ctype);
		var address = myWallet.address;
		//console.log(token);
		//console.log(JSON.stringify(coindata));
		//		console.log(ctype);
		/*		var coindata = ethscan.getCoindata(token);
				var coinname = coindata['name'];
				var ctype = coindata['ctype'];
				var myWallet = ethscan.getWallet(ctype);
				var address = myWallet.address;*/
		//var address = "1111";
		//var out_trade_no = "3123123";
		var sysdata;
		renderdata();

		function telto() {
			var tel = this.innerHTML;
			if(mui.os.plus) {
				plus.device.dial(tel);
			} else {
				location.href = 'tel:' + tel;
			}
		}

		function renderdata() {
			$.getJSON(weburl + 'Home/ChatApi/getTradeInfo?id=' + id, function(res) {
				if(res.status == 1) {
					console.log(JSON.stringify(res.data));

					sysdata = res.data;
					var detail = template('main-template', {
						"trade": res.data,
						"uid": uid,
						"coinname": coinname
					});

					doc.getElementById("main-data").innerHTML = detail;
					doc.getElementById('telephone').addEventListener('tap', telto);
					doc.getElementById('buy_telephone').addEventListener('tap', telto);
				} else {
					doc.getElementById("main-data").innerHTML = res.msg;
				}
			});
		}

		function addnewFriend(fid) {
			console.log(fid);
			var postdata = {
				fid: fid,
				user_id: uid
			};
			console.log(JSON.stringify(postdata));
			mui.post(apiurl + 'user/addfriends', postdata, function(res) {
				console.log(JSON.stringify(res));
				if(res.status > 0) {
					mui.alert("请等待好友通过！");
					var mainid = localStorage.getItem('mainpage');
					var mainWin = plus.webview.getWebviewById(mainid);
					postdata.type = 'addfriends';
					mui.fire(mainWin, 'addFriends', postdata);
				} else {
					mui.alert(res.msg);
				}
			});
		}

		$("#main-data").on("tap", "a", function() {
			var href = this.getAttribute("href");
			var coindata = ethscan.getCoindata(token);
			var coinname = coindata['name'];
			var ctype = coindata['ctype'];
			
			if(this.id == "ethurl") {
				plus.runtime.openURL('https://etherscan.io/tx/' + href);
				return;
			} else if(this.id == "contractsuser") {
				var fid = this.getAttribute("fid");
				//console.log(fid);
				var postdata = {
					keyword: fid,
					user_id: uid
				};

				console.log(JSON.stringify(postdata));
				console.log(apiurl + 'user/searchfriend');
				$.post(apiurl + 'user/searchfriend', postdata, function(res) {
					console.log(JSON.stringify(res));

					if(res.status > 0) {
						addnewFriend(postdata.keyword);
					} else if(res.info) {
						var fid = res.info.f_id;
						var head = res.info.f_avatar;
						var fname = res.info.f_name;
						var phone = res.info.f_phone;
						var targets = '/pages/user/other_detail.html';
						mui.openWindow({
							url: targets,
							id: targets,
							extras: {
								fid: fid,
								head: head,
								fname: fname,
								phone: phone,
								back_id: 'newfriend'
							}
						});
					} else {
						$.alert(res.msg);
					}
				});
				return;
			}

			var targets = weburl + "Home/ChatApi/" + href;
			var passwd = doc.getElementById("passwd").value;

			if(passwd.length < 1 && href != "payTrade.html") {
				$.alert("请输入交易密码");
				return;
			}

			if(href == "payTrade.html") {
				/*
				var postdata = {
					u: uid,
					p1: passwd,
					orderid: id,
					out_trade_no: '',
					wallet: address
				};
				
				plus.nativeUI.showWaiting("订单提交中...");
				$.post(targets, postdata, function(res) {
				
					//$.alert(res.msg);
					plus.nativeUI.closeWaiting();
					if(res && res.status > 0) {*/
						//renderdata();
						ethscan.payToAddrByPwd(sysdata.buy_wallet_addr, ctype, coindata, sysdata.trade_money, function(hash, addr) {
							//console.log(JSON.stringify(hash));
							if(!hash) {
								$.alert("支付失败！");
								return;
							}
							//plus.nativeUI.showWaiting("订单更新中...");

							var postdata = {
								u: uid,
								p1: passwd,
								orderid: id,
								out_trade_no: hash,
								wallet: address
							};
							$.post(targets, postdata, function(res) {
								//plus.nativeUI.closeWaiting();
								renderdata();
								if(res && res.status > 0) {
									$.alert("交易成功！");
									plus.webview.currentWebview().close();
								}
							});

						}, function(err) {
							console.log(JSON.stringify(err));
							$.alert("支付失败！");
						});
						/*
					}else{
						$.alert('提交失败！');
						return;
					}
				});*/

			} else {
				//console.log(JSON.stringify(hash));
				function postdatafunc() {
					var postdata = {
						u: uid,
						p1: passwd,
						orderid: id,
						wallet: address
					};
					var orderhashel = document.getElementById('orderhash');
					if(orderhashel && orderhashel.value && orderhashel.value != '') {
						postdata['hash'] = orderhashel.value;
					}
					plus.nativeUI.showWaiting("订单提交中...");
					$.post(targets, postdata, function(res) {
						plus.nativeUI.closeWaiting();
						$.alert(res.msg);
						var marketWin = plus.webview.getWebviewById('market.html');
						mui.fire(marketWin, 'refreshList', null);

						if(res && res.status > 0) {
							renderdata();
							plus.webview.currentWebview().close();
						}
					});
				}
				//锁定交易
				if(href == 'lockTrade.html') {
					console.log(JSON.stringify(sysdata));
					var coindata = ethscan.getCoindata(sysdata['icon_type']);
					var ctype = coindata['ctype'];
					//console.log(JSON.stringify(coindata));
					//console.log(sysdata.buytype);
					//return;
					//buy 判断真币数量
					if(sysdata.buytype == 1) {
						ethscan.checkPayMoney(sysdata['trade_money'], coindata, function() {
							postdatafunc();
						});
					} else {
						//sell 判断系统币数量
						var url = weburl + "/home/ChatApi/userinfo.html?u=" + uid + "&p=" + postpass;
						//console.log(url);
						mui.getJSON(url, function(res) {
							if(res.status == 1 && res.data.a1_money * 1 > sysdata['amount'] * 1) {
								postdatafunc();
							} else {
								$.alert("金额不足！");
							}
						});

					}
				} else {
					postdatafunc();
				}

			}
		});
	});
}(mui, document));

function openurl(hash) {
	plus.runtime.openURL('https://etherscan.io/tx/' + hash);
}